package artibella.ui.scrolls 
{
	import artibella.IDisposable;
	import artibella.ui.scrolls.AdaptableScroller;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Sprite;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	import caurina.transitions.Tweener;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class Slider extends Sprite implements IDisposable
	{
		private var _stage:Stage;
		
		private var _bar:Sprite;
		private var _scroller:AdaptableScroller;
		
		private var _minimum:Number = 0;
		private var _maximum:Number = 0;
		
		private var _valeur:Number = 0;
		
		public function Slider() 
		{
			
			
			
			_init();
			
			//_activated(true);
		}
		
		private function _init():void
		{
			var barre:Sprite = new Sprite();
			barre.graphics.beginFill( 0xCCCCCC, 0 );
			barre.graphics.drawRect( 0, 0, 50, 5 );
			barre.graphics.endFill();
			barre.graphics.lineStyle( 1, 0xCCCCCC );
			barre.graphics.moveTo( 0, 2 );
			barre.graphics.lineTo( 50, 2 );
			setBar( barre );
			
			var scroller:AdaptableScroller = new AdaptableScroller();
			scroller.graphics.beginFill( 0x000000 );
			scroller.graphics.drawRect( 0, 0, 5, 5 );
			setScroller( scroller );
			
		}
		
		public function setValues( min:Number, max:Number, current:Number ):void
		{
			_minimum = min;
			_maximum = max;
			_valeur = current;
			
			setScrollerLocation( ( current - min ) / ( max - min ) );
			
			_activated(true);
		}
		
		// - - -
		
		
		public function setBar( c:Sprite ):void
		{
			_bar = c;
			addChildAt( _bar, 0 );
		}
		
		public function setScroller( c:AdaptableScroller ):void
		{
			_scroller = c;
			_scroller.y = 0;
			addChild( _scroller );
		}
		
		// - - -
		
		//public function updatedContent( r:Number ):void 
		//{
			//if ( r <= 1 ) {
				//_activated( false );
			//} else {
				//
				//_activated( true );
				//_scroller.y = 0;
					//
				//if (_adaptable ) {
					//var hauteurScroll:Number = _bar.height / _box.ratio;
					//_scroller.setHeight( hauteurScroll );
				//}
			//}
		//}
		
		protected function _activated( b:Boolean ):void
		{
			if( b ) {
				
				if ( _bar != null ) {
					_bar.buttonMode = false;
					_bar.addEventListener( MouseEvent.CLICK, _barHandler );
				}
				
				if ( _scroller != null ) {
					_scroller.buttonMode = true;
					_scroller.addEventListener( MouseEvent.MOUSE_DOWN, _dragHandler );
				}

			} else {
				
				if ( _bar != null ) {
					_bar.buttonMode = false;
					_bar.removeEventListener( MouseEvent.CLICK, _barHandler );
				}
				
				if ( _scroller != null ) {
					_scroller.buttonMode = false;
					_scroller.removeEventListener( MouseEvent.MOUSE_DOWN, _dragHandler );
				}
			}
		}
		
		// - - -
		
		private function _dragHandler( e:MouseEvent ):void
		{
			_scroller.removeEventListener( MouseEvent.MOUSE_DOWN, _dragHandler );
			
			var largeur:Number = _bar.width - _scroller.getWidth();
			var rect:Rectangle = new Rectangle( 0, 0, largeur, 0 );
			_scroller.startDrag( false, rect );
			
			_stage = stage;
			
			_stage.addEventListener( MouseEvent.MOUSE_UP, _dropHandler );
			addEventListener( Event.ENTER_FRAME, _enterFrameHandler );
			
		}
		
		private function _dropHandler( e:MouseEvent ):void
		{
			_stage.removeEventListener( MouseEvent.MOUSE_UP, _dropHandler );
			removeEventListener( Event.ENTER_FRAME, _enterFrameHandler );
			
			_scroller.stopDrag();
			
			_scroller.addEventListener( MouseEvent.MOUSE_DOWN, _dragHandler );
			
			dispatchEvent( new Event( Event.CHANGE ) );
			
		}
		
		private function _enterFrameHandler( e:Event ):void
		{
			var largeur:int = _bar.width - _scroller.getWidth();
			var percent:Number = _scroller.x / largeur;
		
			_setValue( percent );
			
		}
		
		// - - -
		
		private function _barHandler( e:MouseEvent ):void
		{
			var percent:Number = mouseX / _bar.width;
			
			_setValue( percent );
			
			var largeur:int = _bar.width - _scroller.getWidth();
			var posX:int = percent * largeur;
			
			Tweener.addTween( _scroller, { x:posX, time:.5 } );
			
			dispatchEvent( new Event( Event.CHANGE ) );
		}
		
		public function setScrollerLocation( percent:Number ):void
		{
			if ( _scroller ) {
				
				var largeur:int = _bar.width - _scroller.getWidth();
				_scroller.x = percent * largeur;
			}
		}
		
		// - - -
		
		private function _setValue( percent:Number ):void
		{
			_valeur = _minimum + percent * ( _maximum - _minimum );
		}
		
		public function get valeur():Number
		{
			return _valeur;
		}
		
		// - - - IDisposable
		
		public function dispose():void
		{
			_activated( false );
			
			if( _stage ) _stage.removeEventListener( MouseEvent.MOUSE_UP, _dropHandler );
			removeEventListener( Event.ENTER_FRAME, _enterFrameHandler );
			
			_bar = null;
			_scroller = null;
			_stage = null;
		}
	}
}
	